package com.training.service.impl;

import com.training.entity.CourseLike;
import com.training.entity.CourseCollection;
import com.training.mapper.LikeMapper; // 假设你将LikeMapper放在mapper包下
import com.training.mapper.CourseMapper; // 用于更新课程的点赞/收藏数
import com.training.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; // 导入事务注解
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;

@Service
public class LikeServiceImpl implements LikeService {

    private static final Logger logger = LoggerFactory.getLogger(LikeServiceImpl.class);

    private final LikeMapper likeMapper;
    private final CourseMapper courseMapper; // 注入CourseMapper

    @Autowired
    public LikeServiceImpl(LikeMapper likeMapper, CourseMapper courseMapper) {
        this.likeMapper = likeMapper;
        this.courseMapper = courseMapper;
    }

    @Override
    public boolean isCourseLikedByUser(Long courseId, Long studentId) {
        // 查询点赞记录是否存在
        Integer count = likeMapper.checkCourseLike(courseId, studentId);
        return count != null && count > 0;
    }

    @Override
    public boolean isCourseCollectedByUser(Long courseId, Long studentId) {
        // 查询收藏记录是否存在
        Integer count = likeMapper.checkCourseCollection(courseId, studentId);
        return count != null && count > 0;
    }

    @Override
    @Transactional // 确保操作的原子性
    public boolean toggleLikeCourse(Long courseId, Long studentId) {
        boolean isLiked = isCourseLikedByUser(courseId, studentId);
        if (isLiked) {
            // 已点赞，执行取消点赞
            int deletedRows = likeMapper.deleteCourseLike(courseId, studentId);
            if (deletedRows > 0) {
                // 更新课程点赞数 -1
                courseMapper.decrementCourseLikeNum(courseId);
                logger.info("Student {} unliked course {}. Deleted {} rows, decremented like count.", studentId, courseId, deletedRows);
                return false; // 当前状态为未点赞
            }
        } else {
            // 未点赞，执行点赞
            CourseLike courseLike = new CourseLike();
            courseLike.setCourseId(courseId);
            courseLike.setStudentId(studentId);
            courseLike.setCreateTime(LocalDateTime.now());
            int insertedRows = likeMapper.insertCourseLike(courseLike);
            if (insertedRows > 0) {
                // 更新课程点赞数 +1
                courseMapper.incrementCourseLikeNum(courseId);
                logger.info("Student {} liked course {}. Inserted {} rows, incremented like count.", studentId, courseId, insertedRows);
                return true; // 当前状态为已点赞
            }
        }
        logger.warn("Failed to toggle like for course {} by student {}. No rows affected.", courseId, studentId);
        return isLiked; // 操作失败，返回原状态
    }

    @Override
    @Transactional // 确保操作的原子性
    public boolean toggleCollectCourse(Long courseId, Long studentId) {
        boolean isCollected = isCourseCollectedByUser(courseId, studentId);
        if (isCollected) {
            // 已收藏，执行取消收藏
            int deletedRows = likeMapper.deleteCourseCollection(courseId, studentId);
            if (deletedRows > 0) {
                // 更新课程收藏数 -1
                courseMapper.decrementCourseCollNum(courseId);
                logger.info("Student {} uncollected course {}. Deleted {} rows, decremented collect count.", studentId, courseId, deletedRows);
                return false; // 当前状态为未收藏
            }
        } else {
            // 未收藏，执行收藏
            CourseCollection courseCollection = new CourseCollection();
            courseCollection.setCourseId(courseId);
            courseCollection.setStudentId(studentId);
            courseCollection.setCreateTime(LocalDateTime.now());
            int insertedRows = likeMapper.insertCourseCollection(courseCollection);
            if (insertedRows > 0) {
                // 更新课程收藏数 +1
                courseMapper.incrementCourseCollNum(courseId);
                logger.info("Student {} collected course {}. Inserted {} rows, incremented collect count.", studentId, courseId, insertedRows);
                return true; // 当前状态为已收藏
            }
        }
        logger.warn("Failed to toggle collect for course {} by student {}. No rows affected.", courseId, studentId);
        return isCollected; // 操作失败，返回原状态
    }
}